﻿using System;
using System.Collections;
using System.Configuration;
using System.Data;

using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;

using Cn.Edwon.Web.Support;
using Cn.Edwon.Biz;
using Cn.Edwon.Spring;
using Cn.Cellula.Model;
using System.Text.RegularExpressions;
using Cn.Cellula.Biz.Port;
using System.Reflection;
using System.Collections.Generic;
using Cn.Cellula.Biz;
using System.Text;
using Cn.Cellula.Model.System;
using Cn.Edwon.Web.Script;

namespace Cn.Cellula.Website.Ws {
    public partial class _Action : PageSupport {
        IWsBiz biz = BizFactory.Get<IWsBiz>();
        String EmptyStr = "[]";
        Int32 Top = 0;
        TSysActionEO eo = null;
        protected void Page_Load(object sender, EventArgs e) {
            try {
                Top = Convert.ToInt32(Request.QueryString["top"]);
            } catch {
                Top = 0;
            }
            try {
                String action = Request.QueryString["id"];
                eo = biz.Load<TSysActionEO>(action);
                Execute(BuildHql(eo.Hql, Request.QueryString["parms"]), eo.ActionType);
            } catch (Exception ex) {
                //没有找到action对象
                log.Error(ex.Message);
                Execute(null, null);
                return;
            }

        }

        public String BuildHql(String rHql,String rParmStr) {
            log.Debug("Initial Action Hql: " + rHql);
            String parm = rParmStr;
            try {
                if (!String.IsNullOrEmpty(parm)) {
                    String[] parms;
                    parms = parm.Split(',');
                    foreach (String k in parms) {
                        rHql = Regex.Replace(rHql, "\\?", k, RegexOptions.None);
                    }
                }
            } catch (Exception ex) {
                log.Error(ex.Message);
                return null;
            }
            return rHql;

        }

        public String BuildJson(String hql, String rActionType) {
            String json = "";
            if (String.IsNullOrEmpty(hql)) {
                log.Debug("Execute Action Hql: hql undefined");
                json = EmptyStr;
            } else {
                //执行hql
                if (rActionType == CodeTypeDef.Action.Select) {
                    log.Debug("Execute select Action Hql:" + hql);
                    IList<Object> list = (Top>0)?biz.Find(hql,Top):biz.Find<Object>(hql);
                    json = JsUtil.ParseEOList2Json(list);
                } else {
                    log.Debug("Execute update Action Hql:" + hql);
                    json = "[" + biz.ExecuteUpdate(hql) + "]";
                }
            }
            return json;
        }

        public void Execute(String hql, String rActionType) {
            Response.Clear();
            Response.Write(BuildJson(hql,rActionType));
        }
    }
}
